#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shunxudui.h"


//1.创建一个空的队列
sequeue_t *CreateEmptySequeue(){
    sequeue_t* p=(sequeue_t*)malloc(sizeof(sequeue_t));
    if(p == NULL){
        printf("队列分配错误!\n");
        return NULL;
    }
    memset(p,0,sizeof(sequeue_t));
    return p;
}

//2.入列 data代表入列的数据
int InSequeue(sequeue_t *p,datatype data){
    if(p == NULL){
        return -1;
    }
    if(IsFullSequeue(p)){
        printf("队列已满!\n");
        return -1;
    }
    p->data[p->rear]=data;
    p->rear=(p->rear+1)%SEQUEUE_SIZE;
    return 1;
}

//3.判断队列是否为满
int IsFullSequeue(sequeue_t *p){
    if(p == NULL){
        return -1;
    }
    return ((p->rear+1)%SEQUEUE_SIZE == p->front);
}

//4.判断队列是否为空
int IsEmptySequeue(sequeue_t *p){
    if(p == NULL){
        return -1;
    }
    return p->front == p->rear;
}

//5.出列
datatype OutSequeue(sequeue_t *p){
    if(p == NULL){
        return -1;
    }
    if(IsEmptySequeue(p)){
        printf("队列为空!\n");
        return -1;
    }
    int temp=p->data[p->front];
    p->data[p->front]=0;
    p->front=(p->front+1)%SEQUEUE_SIZE;
    return temp;
}

//6.求队列的长度
int LengthSequeue(sequeue_t *p){
    if(p == NULL){
        return -1;
    }
    return abs(p->front-p->rear);
}

//7.清空队列函数
void ClearSequeue(sequeue_t *p){
    if(p == NULL){
        return;
    }
    memset(p->data,0,sizeof(datatype)*SEQUEUE_SIZE);
    p->front = 0;
    p->rear = 0;
}